package com.yiqi.lottery.feature.find.view;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.suozhang.framework.component.recyclerview.DividerItemDecoration;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.widget.EmptyView;
import com.yiqi.lottery.R;
import com.yiqi.lottery.entity.bo.LotteryHistoryBo;
import com.yiqi.lottery.entity.bo.LotteryResultBo;
import com.yiqi.lottery.entity.enums.LotteryType;
import com.yiqi.lottery.entity.enums.ShowLotteryType;
import com.yiqi.lottery.feature.find.contract.ResultContract;
import com.yiqi.lottery.feature.find.dependencies.result.DaggerResultComponent;
import com.yiqi.lottery.feature.find.dependencies.result.ResultPresenterModule;
import com.yiqi.lottery.feature.find.view.adapter.LotteryHistoryAdapter;
import com.yiqi.lottery.feature.find.view.adapter.LotteryResultAdapter;

import javax.inject.Inject;
import java.util.List;

public class ResultViewActivity extends BaseActivity implements ResultContract.View, BaseQuickAdapter.OnItemClickListener, OnRefreshListener {

    @BindView(R.id.toolbar)
    Toolbar mToolbar;

    @Inject
    ResultContract.Presenter mPresenter;
    @BindView(R.id.rv_lottery_history)
    RecyclerView mRvLotteryHistory;
    @BindView(R.id.smartRefresh)
    SmartRefreshLayout mSmartRefresh;

    private int position = 0;
    private LotteryHistoryAdapter mAdapter;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_result_view;
    }

    @Override
    protected void initInjector() {
        DaggerResultComponent.builder().resultPresenterModule(new ResultPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {

        position = this.getIntent().getIntExtra("type", 0);
        initToolBar(mToolbar, ShowLotteryType.getName(position), true, true);
        initAdapter(position);


    }

    @Override
    protected void initData() {
        mPresenter.getLotteryHistory(LotteryType.getName(position));
    }


    @Override
    public void showLotteryHistorySuccess(List<LotteryHistoryBo> resultBos) {
        mSmartRefresh.finishRefresh();
        mAdapter.setNewData(resultBos);
    }

    @Override
    public void showLotteryError(String error) {
        mSmartRefresh.finishRefresh();
        mAdapter.setNewData(null);
    }

    @Override
    public void showLotteryEmpty() {
        mSmartRefresh.finishRefresh();
        mAdapter.setNewData(null);
    }

    private void initAdapter(int position) {
        mSmartRefresh.setEnableLoadMore(false);
        mAdapter = new LotteryHistoryAdapter(position);
        mAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);
        mRvLotteryHistory.setLayoutManager(new LinearLayoutManager(this));
        mRvLotteryHistory.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(2));
        mAdapter.bindToRecyclerView(mRvLotteryHistory);
        mAdapter.setOnItemClickListener(this);
        mSmartRefresh.setOnRefreshListener(this);
    }


    @Override
    public void showLotteryResultSuccess(List<LotteryResultBo> resultBos) {


    }


    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        initData();
    }
}
